﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_ccWasteAnalysis_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);
            if (AcmsContext.CurrentPost != null)
            {
                lnkAuditOne.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../WasteAudit?Audit=1");
                lnkAuditTwo.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../WasteAudit?Audit=2");
                lnkPupilNumbers.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../");
                lnkEnergyAudit.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../EnergyAudit?Audit=1");
            }

            DataBind();
        }
    }

    public override void DataBind()
    {
        bool hasPupilNumbersEntered = false;
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);

        if (audits.Count >= 1 && audits.First().NoPupils > 0)
        {
            hasPupilNumbersEntered = true;
        }

        if (!hasPupilNumbersEntered)
        {
            Response.Redirect(AcmsContext.CurrentPost.Parent.Url);
        }
        else if (audits.Count != 2 || !audits.First().WasteAuditCompleted)
        {
            Response.Redirect(AcmsContext.CurrentPost.ResolvePath("../WasteAudit?Audit=1"));
        }
        else if (!audits.Skip(1).First().WasteAuditCompleted == null)
        {
            Response.Redirect(AcmsContext.CurrentPost.ResolvePath("../WasteAudit?Audit=2"));
        }
        else
        {
            var first = audits.ElementAt(0);
            var second = audits.ElementAt(1);
            bool totalSaving = false;

            var totalDifference = (second.TotalWasteCO2PerDay - first.TotalWasteCO2PerDay) / first.TotalWasteCO2PerDay;
            var totalPerDay = second.TotalWasteCO2PerDay - first.TotalWasteCO2PerDay;
            var totalPerYear = second.TotalWasteCO2PerYear - first.TotalWasteCO2PerYear;

            if(totalDifference < 0)
            {
                totalSaving = true;
                totalDifference = totalDifference * -1;
                totalPerDay = totalPerDay * -1;
                totalPerYear = totalPerYear * -1;
            }

            bool recyleSaving = false;
            var recyleDifference = (second.TotalRecyleCO2PerDay - first.TotalRecyleCO2PerDay) / first.TotalRecyleCO2PerDay;
            var recylePerDay = second.TotalRecyleCO2PerDay - first.TotalRecyleCO2PerDay;
            var recylePerYear = second.TotalRecyleCO2PerSchoolYear - first.TotalRecyleCO2PerSchoolYear;
            if (recyleDifference < 0)
            {
                recyleSaving = true;
                recyleDifference = recyleDifference * -1;
                recylePerDay = recylePerDay * -1;
                recylePerYear = recylePerYear * -1;
            }

            bool landSaving = false;
            var landDifference = (second.TotalLandCO2PerDay - first.TotalLandCO2PerDay) / first.TotalLandCO2PerDay;
            var landPerDay = second.TotalLandCO2PerDay - first.TotalLandCO2PerDay;
            var landPerYear = second.TotalLandCO2PerSchoolYear - first.TotalLandCO2PerSchoolYear;
            if (landDifference < 0)
            {
                landSaving = true;
                landDifference = landDifference * -1;
                landPerDay = landPerDay * -1;
                landPerYear = landPerYear * -1;
            }

            //format percentage to two decimal places
            lblTotalPercentage.Text = string.Format("{0:0.##%} {1}", totalDifference, totalSaving ? "less" : "more");
            lblRecyledPercentage.Text = string.Format("{0:0.##%} {1}", recyleDifference, recyleSaving ? "less" : "more");
            lblLandFillPercentage.Text = string.Format("{0:0.##%} {1}", landDifference, landSaving ? "less" : "more");

            //show difference per day as two decimal places
            lblTotalPerDay.Text = string.Format("{0} {1:0.##}", totalSaving ? "Saving" : "An extra", totalPerDay);
            lblRecyledPerDay.Text = string.Format("{0} {1:0.##}", recyleSaving ? "Saving" : "An extra", recylePerDay);
            lblLandFillPerDay.Text = string.Format("{0} {1:0.##}", landSaving ? "Saving" : "An extra", landPerDay);

            //show difference per week as party balloons to zero decimal places
            lblTotalBalloons.Text = (totalPerDay * 1306).ToString("0");
            lblRecyledBalloons.Text = (recylePerDay * 1306).ToString("0");
            lblLandFillBalloons.Text = (landPerDay * 1306).ToString("0");
            
            //show difference per year to two decimal places
            lblTotalPerYear.Text = string.Format("{0} {1:0.##}", totalSaving ? "Saving" : "An extra", totalPerYear);
            lblRecyledPerYear.Text = string.Format("{0} {1:0.##}", recyleSaving ? "Saving" : "An extra", recyleDifference);
            lblLandFillPerYear.Text = string.Format("{0} {1:0.##}", landSaving ? "Saving" : "An extra", landDifference);
            
            //show difference per year as planes to zero decimal places
            lblTotalPlanes.Text = (totalPerYear * 1.225).ToString("0");
            lblRecyledPlanes.Text = (recylePerYear * 1.225).ToString("0");
            lblLandFillPlanes.Text = (landPerYear * 1.225).ToString("0");

            flashTotal.Variables["Audit1"] = first.TotalWasteCO2PerDay.ToString();
            flashTotal.Variables["Audit2"] = second.TotalWasteCO2PerDay.ToString();

            flashRecyled.Variables["Audit1"] = first.TotalRecyleCO2PerDay.ToString();
            flashRecyled.Variables["Audit2"] = second.TotalRecyleCO2PerDay.ToString();

            flashLandfill.Variables["Audit1"] = first.TotalLandCO2PerDay.ToString();
            flashLandfill.Variables["Audit2"] = second.TotalLandCO2PerDay.ToString();

        }
    }
}
